<?php
namespace app\common\logic;

use app\common\component\help_get\HelpGetModel;
use app\common\component\help_to\HelpToService;
use app\common\component\help_to\HelpToModel;

class Diggings {
	public function getHelpGets($min, $max, $is_new = false) {
		$HelpGetModel = new HelpGetModel(); 
		
		$HelpGetModel -> alias('h_g')
			-> join('league_user u', 'h_g.user_id = u.user_id')
			-> field(
				'h_g.help_get_id, h_g.help_to_id, h_g.user_id h_g_user_id, h_g.order_id, h_g.money, h_g.create_time, h_g.begin_time, h_g.status, ' .
				'u.username, u.realname, u.phone, u.bank_code'
			)
			-> where('h_g.status = 9 AND h_g.money >= ' . $min . ' AND h_g.money <= ' . $max)
			-> order('help_get_id ASC');

		if($is_new) {
			$HelpGetModel -> where('h_g.user_id', 1);
			$HelpGetModel -> where('h_g.user_id', '<>', \think\Session::get('user_id'));
		} else if(\think\Session::has('user_id')) {
			$HelpGetModel -> where('h_g.user_id', '<>', 1);
			$HelpGetModel -> where('h_g.user_id', '<>', \think\Session::get('user_id'));
		}

		return $HelpGetModel -> select();
	}

	public function grabHlepGet($help_get_id, $action) {
		$currentTime = time();
		$userId = \think\Session::get('user_id');
		$checkHelpTo = HelpToModel::get(['user_id' => $userId]);
		switch($action) {
			case 1:
				if($checkHelpTo) {
					return ['code' => -1, 'message' => '抱歉！你已经无法在新手区抢单！'];
				}
				break;
			default:
				if(!$checkHelpTo) {
					return ['code' => -1, 'message' => '抱歉！请先从到新手区抢单！'];
				}
				break;
		}

		$getHelpGet = HelpGetModel::get(['help_get_id' => $help_get_id]);

		if($getHelpGet -> getData('status') != 9) {
			return ['code' => -1, 'message' => '抱歉！订单已被抢了！'];
		}
		$getHelpToLast = HelpToModel::where('user_id', $userId) -> order('begin_time DESC') -> find();
		if($getHelpToLast) {
			// 判断冷却时间
			if(\think\Session::get('is_vip')) {
				$freezeTime = $getHelpToLast -> begin_time + getParam('GET_HELP_GET_FREEZE_TIME_VIP') * 60;
			} else {
				$freezeTime = $getHelpToLast -> begin_time + getParam('GET_HELP_GET_FREEZE_TIME') * 60;
			}

			if($freezeTime > $currentTime) {
				return ['code' => -1, 'message' => '抱歉！抢单冷却中，请稍后再抢单!'];
			}
		}

		$helpToData = [];
		$helpToData['help_get_id'] = $getHelpGet -> help_get_id;
		$helpToData['user_id'] = $userId;
		$helpToData['money'] = $getHelpGet -> money;
		$helpToData['create_time'] = $helpToData['begin_time'] = $currentTime;
		$helpToData['status'] = 1;
		$helpToData['help_get_from'] = 1;

		$getHelpGet -> begin_time = $currentTime;
		$getHelpGet -> status = 1;
		\think\Db::startTrans();
		try {
			$resultHelpTo = (new HelpToService()) -> create($helpToData);
			if($resultHelpTo['code'] != 0) {
				throw new \Exception($resultHelpTo['message']);
			}

			$getHelpGet -> help_to_id = $resultHelpTo['data']['help_to'] -> help_to_id;

			if(!$getHelpGet -> save()) {
				throw new \Exception('抢单提现修改失败');
			}
			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			//\think\Log::error($e -> getMessage());
			return ['code' => -1, 'message' => $e -> getMessage()];
		}

		return ['code' => 0, 'message' => '抢单成功，请立刻打款！'];
	}
}
?>